package com.zyc.studentstudy.utils

import android.content.Context
import android.net.ConnectivityManager
import android.net.wifi.WifiManager

object WifiUtils {
    /**
     * 获取当前热点最新的信号强度
     * 一个0到-100的区间值，是一个int型数据，其中0到-50表示信号最好，-50到-70表示信号偏差，小于-70表示最差，有可能连接不上或者掉线
     *
     * @param mContext
     * @return
     */
    @JvmStatic
    fun getCurrentNetworkRssi(mContext: Context): String {
        val wifiManager = mContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
        val wifiInfo = wifiManager.connectionInfo
        return if (isWifiConnected(mContext)) wifiInfo.rssi.toString() else "disconnect or not enable"
    }

    /**
     * wifi是否连接
     *
     * @param context
     * @return
     */
    @JvmStatic
    fun isWifiConnected(context: Context): Boolean {
        val connectivityManager = context
            .getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        val networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
        if (null != networkInfo && networkInfo.isConnected) {
            return true
        }
        return false
    }
}
